/* eslint-disable react-hooks/exhaustive-deps */
import React, { useEffect } from 'react';
import {
  useRoutes, useLocation, useNavigate,
} from 'react-router-dom';


const Index = React.lazy(() => import('./pages/index'));
const Account = React.lazy(() => import('./pages/account/index'));
const Login = React.lazy(() => import('./pages/account/login/index'));

export default function Router() {
  const location = useLocation();
  const navigator = useNavigate();
  // const [params] = useRouter();
  useEffect(() => {
    const token = location.search?.match(/token.*/)?.[0]?.replace('token=', '');
    if (token) {
      localStorage[import.meta.env.VITE_TOKEN_KEY] = token;
      navigator('/index');
    }
  }, [location]);
  const element = useRoutes([
    {
      path: '/account',
      element: <Account />,
      children: [
        { path: 'login', element: <Login /> },
      ],
    },
    {
      path: '/',
      element: <Index />,
      // element: <Auth><Index /></Auth>,
    },
  ]);
  return element;
}
